import Vue from 'vue'
import VueRouter from 'vue-router'
import Login from '@/views/Login/Login'
import Reg from '@/views/Reg/Reg'
import Main from '@/views/Main/Main'
import store from '../store'
import Home from '@/views/Menus/Home/Home'
import UserInfo from '@/views/Menus/User/UserInfo'
import UserAvatar from '@/views/Menus/User/UserAvatar'
import UserPwd from '@/views/Menus/User/UserPwd'
import ArtCate from '@/views/Menus/Article/ArtCate'

Vue.use(VueRouter)

const routes = [
  {
    path:'/login',
    component:Login
  },
  {
    path:'/reg',
    component:Reg
  },
  {
    path:'/',
    component:Main,
    redirect: '/home',
    children:[
     {
      path:'home',
      component:Home
     },
     {
      path:'user-info',
      component:UserInfo
     },
     {
      path:'user-avatar',
      component:UserAvatar
     },
     {
      path:'user-pwd',
      component:UserPwd
     },
     {
      path:'art-cate',
      component:ArtCate
     }
    ]
  }
]

const router = new VueRouter({
  routes
})


router.beforeEach((to, from, next) =>{
  const token = store.state.token
  if(!token && to.path !== '/login' && to.path !== '/reg') {
    next('/login')
  } else {
    next()
  }
})
export default router
